package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.UserInfo;

import java.util.List;

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {

    private final List<UserInfo> userList;
    private final Context context;
    private final OnUserActionListener listener;

    public UserAdapter(Context context, List<UserInfo> userList) {
        this.context = context;
        this.userList = userList;
        
        if (context instanceof OnUserActionListener) {
            this.listener = (OnUserActionListener) context;
        } else {
            throw new RuntimeException(context.toString() + " must implement OnUserActionListener");
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user_original, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        UserInfo user = userList.get(position);
        
        holder.tvUserId.setText("ID: " + user.getId());
        holder.tvUsername.setText(user.getUsername());
        holder.tvNickname.setText(user.getNickname());
        holder.tvPhone.setText(user.getPhone());
        holder.tvBalance.setText("¥" + user.getBalance());
        holder.tvUserLevel.setText(user.getLevel());
        
        // 设置按钮点击事件
        holder.btnEditUser.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEditUser(user);
            }
        });
        
        holder.btnBanUser.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDeleteUser(user);
            }
        });
        
        holder.btnViewTasks.setOnClickListener(v -> {
            if (listener != null) {
                listener.onViewTasks(user);
            }
        });
    }

    @Override
    public int getItemCount() {
        return userList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvUserId;
        TextView tvUsername;
        TextView tvNickname;
        TextView tvPhone;
        TextView tvBalance;
        TextView tvUserLevel;
        Button btnEditUser;
        Button btnBanUser;
        Button btnViewTasks;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvUserId = itemView.findViewById(R.id.tv_user_id);
            tvUsername = itemView.findViewById(R.id.tv_username);
            tvNickname = itemView.findViewById(R.id.tv_nickname);
            tvPhone = itemView.findViewById(R.id.tv_phone);
            tvBalance = itemView.findViewById(R.id.tv_balance);
            tvUserLevel = itemView.findViewById(R.id.tv_user_level);
            btnEditUser = itemView.findViewById(R.id.btn_edit_user);
            btnBanUser = itemView.findViewById(R.id.btn_ban_user);
            btnViewTasks = itemView.findViewById(R.id.btn_view_tasks);
        }
    }

    public interface OnUserActionListener {
        void onEditUser(UserInfo user);
        void onDeleteUser(UserInfo user);
        void onViewTasks(UserInfo user);
    }
}